package org.cid54.jshopper.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.cid54.jshopper.model.User;
import org.cid54.jshopper.model.UserGroup;
import org.cid54.jshopper.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.RedirectView;

/**
 *
 * @author fgo
 */
@Controller
@RequestMapping("admin")
@SessionAttributes({"userName"})
public class AdminController {

    @Autowired
    private AdminService adminService;

    private View getRedirect(HttpServletRequest request) {
        return new RedirectView(request.getContextPath() + "/mvc/admin");
    }

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView doGet() {

        ModelAndView mav = new ModelAndView("admin");

        mav.addObject("users", adminService.getAllUsers());

        mav.addObject("groups", adminService.getAllGroups());

        mav.addObject("memberships", adminService.getUsersMemberShips());

        mav.addObject("items", adminService.getAllItems());

        return mav;

    }

    @RequestMapping(value = "item/update", method = RequestMethod.POST)
    public View doItemUpdate(HttpServletRequest request) {

        long itemId = Long.parseLong(request.getParameter("id"));

        this.adminService.updateItem(itemId, request.getParameter("name"));

        return this.getRedirect(request);

    }

    @RequestMapping(value = "item/delete", method = RequestMethod.POST)
    public View doItemDelete(HttpServletRequest request) {

        this.adminService.deleteItem(Long.parseLong(request.getParameter("id")));

        return this.getRedirect(request);

    }

    @RequestMapping(value = "item/new", method = RequestMethod.POST)
    public View doItemCreate(HttpServletRequest request) {

        this.adminService.createItem(request.getParameter("name"));

        return this.getRedirect(request);

    }

    @RequestMapping(value = "user/update", method = RequestMethod.POST)
    public View doUserUpdate(HttpServletRequest request) {

        long userId = Long.parseLong(request.getParameter("id"));

        User user = new User();

        user.setName(request.getParameter("name"));

        user.setAdmin("on".equals(request.getParameter("admin")));

        user.setPassword(request.getParameter("new_password"));

        List<Long> userGroupIds = new ArrayList<Long>();

        for (UserGroup group : this.adminService.getAllGroups()) {
            if ("on".equals(request.getParameter("" + group.getId()))) {
                userGroupIds.add(group.getId());
            }
        }

        this.adminService.updateUser(userId, user, userGroupIds);

        return this.getRedirect(request);

    }

    @RequestMapping(value = "user/create", method = RequestMethod.POST)
    public View doUserCreate(HttpServletRequest request) {

        User user = new User();

        user.setName(request.getParameter("name"));

        user.setAdmin("on".equals(request.getParameter("admin")));

        user.setPassword(request.getParameter("new_password"));

        List<Long> userGroupIds = new ArrayList<Long>();

        for (UserGroup group : this.adminService.getAllGroups()) {
            if ("on".equals(request.getParameter("" + group.getId()))) {
                userGroupIds.add(group.getId());
            }
        }

        this.adminService.createUser(user, userGroupIds);

        return this.getRedirect(request);

    }

    @RequestMapping(value = "user/delete", method = RequestMethod.POST)
    public View doUserDelete(HttpServletRequest request) {

        this.adminService.deleteUser(Long.parseLong(request.getParameter("id")));

        return this.getRedirect(request);

    }

    @RequestMapping(value = "group/update", method = RequestMethod.POST)
    public View doGroupUpdate(HttpServletRequest request) {

        long groupId = Long.parseLong(request.getParameter("id"));

        this.adminService.updateGroup(groupId, request.getParameter("name"));

        return this.getRedirect(request);

    }

    @RequestMapping(value = "group/create", method = RequestMethod.POST)
    public View doGroupCreate(HttpServletRequest request) {

        UserGroup group = new UserGroup();

        group.setName(request.getParameter("name"));

        this.adminService.createGroup(group);

        return this.getRedirect(request);

    }

    @RequestMapping(value = "group/delete", method = RequestMethod.POST)
    public View doGroupDelete(HttpServletRequest request) {

        this.adminService.deleteGroup(Long.parseLong(request.getParameter("id")));

        return this.getRedirect(request);

    }
}
